package com.panopset;

import java.io.File;
import java.io.StringWriter;

import com.panopset.compat.RezIO;

/**
 * Creates cp.props file which has CP property set to the classpath of the
 * current project pom.xml.
 *
 * Useful in ant scripts.
 *
 * @author Karl Dinwiddie
 *
 */
public final class Pom2classpathProps {

    /**
     * @param args
     *            Not used
     * @throws Exception Exception.
     */
    public static void main(final String[] args) throws Exception {
        new Pom2classpathProps().go();
    }

    /**
     * Go.
     *
     * @throws Exception Exception.
     */
    private void go() throws Exception {
        final StringWriter sw = new StringWriter();
        boolean firstTime = true;
        for (MavenAuditor.MavenDependency d : new MavenAuditor()
                .loadPomDependencies(new File(PomParser.POM_FILE))) {
            if (d.getScope().trim().equalsIgnoreCase("test")) {
                continue;
            }
            if (firstTime) {
                firstTime = false;
            } else {
                sw.append(";");
            }
            sw.append(d.getLocalRepositoryJar());
        }
        String rtn = sw.toString().replace("\\", "/");
        File f = new File("cp.props");
        if (!UtilIO.saveStringToFile("CP=" + rtn, f)) {
            Alert.red(Util.x("Error writing to") + " "
                    + RezIO.getCanonicalPath(f));
        }
    }

    /**
     * Utility class.
     */
    private Pom2classpathProps() { }
}
